package com.example.wallpaper;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wallpaper.databinding.FragmentWallListBinding;
import com.example.wallpaper.databinding.ItemWallpaperBinding;
import com.example.wallpaper.model.Wallpaper;
import com.example.wallpaper.photoapi.DmoeCatch;
import com.example.wallpaper.vm.WallListVM;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class WallFragment extends Fragment {

    //初始化加载的最大图片数量
    private static final int WALL_SIZE=50;

    private List<Wallpaper> mWallpapers;

    private RecyclerView mWallRecyclerview;

    private WallAdapter mWallAdapter;

    private Callbacks mCallbacks;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化占位图的加载
        initWallpaper();
    }

    /**
     * 当fragment和activity建立关联时会调用
     * @param context 指的就是托管的activity
     * */
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mCallbacks = (Callbacks) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        FragmentWallListBinding binding = DataBindingUtil.inflate(inflater,R.layout.fragment_wall_list,container,false);
        mWallRecyclerview = binding.wallListRecyclerview;
        mWallRecyclerview.setLayoutManager(new GridLayoutManager(getActivity(),3));
        mWallAdapter = new WallAdapter();
        mWallRecyclerview.setAdapter(mWallAdapter);
        return binding.getRoot();
    }


    private class WallAdapter extends RecyclerView.Adapter<WallViewHolder>{


        @NonNull
        @Override
        public WallViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            ItemWallpaperBinding binding = DataBindingUtil.inflate(inflater,R.layout.item_wallpaper,parent,false);
            WallViewHolder viewHolder = new WallViewHolder(binding);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull WallViewHolder holder, int position) {
            Wallpaper wallpaper = mWallpapers.get(position);
            holder.bind(wallpaper,position);
        }

        @Override
        public int getItemCount() {
            return mWallpapers.size();
        }
    }

    private class WallViewHolder extends RecyclerView.ViewHolder{

        private ItemWallpaperBinding mBinding;

        public WallViewHolder(ItemWallpaperBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }

        public void bind(Wallpaper wallpaper,int position){
            String imgUrl = wallpaper.getImgUrl();
            if(imgUrl==null){
                DmoeCatch dmoeCatch = new DmoeCatch();
                WallpaperHandler handler = new WallpaperHandler();
                dmoeCatch.getWallpaper(handler,position);
            }
            WallListVM wallListVM = new WallListVM(mCallbacks,imgUrl,wallpaper.getBitDrawable());
            mBinding.setWallListVM(wallListVM);
//            mBinding.wallpaperItem.setImageDrawable(wallpaper.getBitDrawable());
        }
    }

    public void initWallpaper(){
        mWallpapers = new ArrayList<>(WALL_SIZE);
        for(int i=0;i<WALL_SIZE;i++){
            Wallpaper wallpaper = new Wallpaper();
            Drawable drawable = getResources().getDrawable(R.drawable.wall_demo, null);
            wallpaper.setBitDrawable(drawable);
            mWallpapers.add(wallpaper);
        }
    }

    private class WallpaperHandler extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            Wallpaper wallpaper = (Wallpaper)bundle.getSerializable("args");
            //缩略图处理
            BitmapDrawable drawable = new BitmapDrawable(getResources(), wallpaper.getBitmap());
            wallpaper.setBitDrawable(drawable);
            int position = bundle.getInt("position");
            //替换集合中的某个索引的对象
            mWallpapers.set(position,wallpaper);
            //通知Recycerview更新UI展示
            mWallAdapter.notifyItemChanged(position);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mCallbacks = null;
    }

    public interface Callbacks{
        void onDetail(String imgUrl);
    }
}
